using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using MUSA;

namespace MUSA
{
    class Explosion
    {
        #region Variables

        Texture2D[] image;
        Vector2 position;

        int index;
        float time;

        bool visible;

        #endregion

        #region Properties

        public bool Visible
        {
            get
            {
                return visible;
            }
        }

        #endregion;

        #region Constructor

        public Explosion(Vector2 _pos)
        {
            position = _pos;
            image = new Texture2D[3];
            image[0] = MUSA.mContentManager.Load<Texture2D>("Media\\explosion1");
            image[1] = MUSA.mContentManager.Load<Texture2D>("Media\\explosion2");
            image[2] = MUSA.mContentManager.Load<Texture2D>("Media\\explosion3");

            index = 0;
            time = 0;
            visible = true;
        }

        #endregion

        #region Methods

        public void Update(GameTime gtime)
        {
            time += (float)gtime.ElapsedGameTime.TotalSeconds;

            if (time > 0.1f)
            {
                time -= 0.1f;
                index++;

                if (index == 3)
                {
                    visible = false;
                }
            }
        }

        public void Draw(GameTime gtime, SpriteBatch sb)
        {
            if (!visible)
                return;

            Vector2 newPos = position;
            newPos.X -= image[index].Width / 2;
            newPos.Y -= image[index].Height / 2;

            sb.Draw(image[index], newPos, Color.White);
        }

        #endregion
    }

    public class ExplosionManager
    {
        List<Explosion> explosions;
        SpriteBatch spriteBatch;

        public ExplosionManager()
        {
            explosions = new List<Explosion>();
            spriteBatch = new SpriteBatch(MUSA.mGraphicsDevice);
        }

        public void Update(GameTime gtime)
        {
            List<Explosion> cullList;
            cullList = new List<Explosion>();

            foreach (Explosion e in explosions)
            {
                e.Update(gtime);

                if (!e.Visible)
                    cullList.Add(e);
            }

            foreach (Explosion e in cullList)
            {
                explosions.Remove(e);
            }
        }

        public void Draw(GameTime gtime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

            foreach (Explosion e in explosions)
            {
                e.Draw(gtime, spriteBatch);
            }

            spriteBatch.End();
        }

        public void AddExplosion(Vector3 _pos)
        {
            Vector2 screenPos = new Vector2(0);

            screenPos.X = ((_pos.X + (MUSA.mCamera.CameraWidth / 2)) / (MUSA.mCamera.CameraWidth))
                       * MUSA.mGraphicsDevice.Viewport.Width;
            screenPos.Y = ((_pos.Y - (MUSA.mCamera.CameraHeight / 2)) / (-1 * MUSA.mCamera.CameraHeight))
                * MUSA.mGraphicsDevice.Viewport.Height;


            Explosion e = new Explosion(screenPos);
            explosions.Add(e);
        }
    }
}
